import { Behaviour, getGameObjectById, Transform, core, HitTestScript, TextRenderer, GameObject } from "../gameengine";
import { ColliderSystem } from "./ColliderSystem";
import { RectCollider, RectColliderType } from "./RectCollider";

//import { mainrole, MainRoleMovingBottom, MainRoleMovingTop, MainRoleMovingLeft, MainRoleMovingRight } from "../zch/MainRole";
import { text, a, s, } from "../data";
import { user } from "../user";

//import { mainrole, MainRoleMovingBottom, MainRoleMovingRight, MainRoleMovingLeft, MainRoleMovingTop } from "../zch/MainRole";




export class MainRoleBehaviour extends Behaviour {

    public Direction: number;

    gameobject: GameObject;

    private speedX = 0;
    private speedY = 0;
    private flag = 0;

    ui1: GameObject;
    ui2: GameObject;


    protected onStart() {

        const mainrole = getGameObjectById("MainRole").getScript(Transform);
        mainrole.x = user.nextEntryPositonX;
        mainrole.y = user.nextEntryPositonY;                       //传送点

        core.loadPrefab("assets/animation/down.prefab.json", (prefabGameObject) => {
            this.displayObject.addChild(prefabGameObject);
            this.gameobject = prefabGameObject;
        })

        console.log('从今天起，你就是主角了');

        const DOWN = 40;
        const LEFT = 37;
        const UP = 38;
        const RIGHT = 39;
        const A = 65;
        const S = 83;

        this.Direction = RIGHT;

        window.onkeydown = (ev) => {
            const SPEED = 4;
            switch (ev.keyCode) {
                case DOWN:
                    if (this.Direction != DOWN) {
                        this.Direction = DOWN;
                        this.displayObject.removeChild(this.gameobject);
                        core.loadPrefab("assets/animation/down.prefab.json", (prefabGameObject) => {
                            this.displayObject.addChild(prefabGameObject);
                            this.gameobject = prefabGameObject;
                        })

                    }
                    this.speedY = SPEED;


                    ev.preventDefault();
                    //mainrole.changeState(new MainRoleMovingBottom());
                    break;
                case UP:
                    if (this.Direction != UP) {
                        this.Direction = UP;
                        this.displayObject.removeChild(this.gameobject);
                        core.loadPrefab("assets/animation/up.prefab.json", (prefabGameObject) => {
                            this.displayObject.addChild(prefabGameObject);
                            this.gameobject = prefabGameObject;
                        })

                    }
                    this.speedY = -SPEED;
                    ev.preventDefault();
                    //  mainrole.changeState(new MainRoleMovingTop());
                    break;
                case LEFT:
                    if (this.Direction != LEFT) {
                        this.Direction = LEFT;
                        this.displayObject.removeChild(this.gameobject);
                        core.loadPrefab("assets/animation/left.prefab.json", (prefabGameObject) => {
                            this.displayObject.addChild(prefabGameObject);
                            this.gameobject = prefabGameObject;
                        })

                    }
                    this.speedX = -SPEED;
                    ev.preventDefault();
                    // mainrole.changeState(new MainRoleMovingLeft());
                    break;
                case RIGHT:
                    if (this.Direction != RIGHT) {
                        this.Direction = RIGHT;
                        this.displayObject.removeChild(this.gameobject);
                        core.loadPrefab("assets/animation/right.prefab.json", (prefabGameObject) => {
                            this.displayObject.addChild(prefabGameObject);
                            this.gameobject = prefabGameObject;
                        })

                    }
                    this.speedX = SPEED;
                    ev.preventDefault();
                    break;
                case A:
                    if (!this.ui1) {
                        this.ui1 = core.createPrefab(a);
                        const scene = getGameObjectById("UIScene");
                        scene.addChild(this.ui1);
                    }
                    ev.preventDefault();
                    break;
                case S:
                    if (!this.ui2) {
                        this.ui2 = core.createPrefab(s);
                        const scene = getGameObjectById("UIScene");
                        scene.addChild(this.ui2);
                    }
                    ev.preventDefault();
                    break;
            }
        }

        window.onkeyup = (ev) => {
            switch (ev.keyCode) {
                case DOWN:
                    this.speedY = 0;
                    ev.preventDefault();
                    //mainrole.changeState(new MainRoleMovingBottom());
                    break;

                case UP:
                    this.speedY = 0;
                    ev.preventDefault();
                    // mainrole.changeState(new MainRoleMovingTop());
                    break;
                case LEFT:
                case RIGHT:
                    this.speedX = 0;
                    ev.preventDefault();
                    break;
                case A:
                    this.ui1.parent.removeChild(this.ui1);
                    this.ui1 = null;
                    ev.preventDefault();
                    break;
                case S:
                    this.ui2.parent.removeChild(this.ui2);
                    this.ui2 = null;
                    ev.preventDefault();
                    break;
            }
        }
    }

    protected onUpdate(advancedTime: number) {
        const space = 32;

        const transform = this.displayObject.getScript(Transform);
        const colliderSystem = getGameObjectById("Scene").getScript(ColliderSystem);
        const collider = this.displayObject.getScript(RectCollider);

        const nextCollider = {
            left: collider.left + this.speedX,
            right: collider.right + this.speedX,
            top: collider.top + this.speedY,
            bottom: collider.bottom + this.speedY
        }
        const hitTestResult = colliderSystem.check(nextCollider, RectColliderType.SHOP);
        const hitTestResult1 = colliderSystem.check(nextCollider, RectColliderType.ENEMY);
        const hitTestResult2 = colliderSystem.check(nextCollider, RectColliderType.WALL);
        if (hitTestResult2) {
        }

        if (hitTestResult) {
            this.speedX = -1;
            this.speedY = 0;

        }
        else {
            if (hitTestResult1) {
                this.speedX = 0;
                this.speedY = 0;
            } else {
                if (hitTestResult2) {

                } else {
                    transform.x += this.speedX;
                    transform.y += this.speedY;
                }
            }

        }





    }


}
